using System;
using System.Collections.Generic;
using System.Text;

namespace BetaIto.ChaosDAV
{
    public static class CfsUtility
    {
        public const string PATH_SEPARATOR = "/";

        public static bool IsRoot(string path1)
        {
            return (path1 == "/");
        }
        public static bool IsFullPath(string path1)
        {
            return path1.StartsWith("/");
        }
        public static string GetFileName(string fullPath)
        {
            if (fullPath.EndsWith(PATH_SEPARATOR))
            {
                fullPath = fullPath.Substring(0, fullPath.Length - 1);
            }
            if (fullPath.IndexOf(PATH_SEPARATOR) < 0)
            {
                return fullPath;
            }
            string fileName = fullPath.Substring(fullPath.LastIndexOf(PATH_SEPARATOR) + 1);
            return fileName;
        }

        public static string ConbinePath(string path1, string path2)
        {
            if (string.IsNullOrEmpty(path1))
            {
                if (string.IsNullOrEmpty(path2))
                {
                    return "";
                }
                return path2;
            }
            else if (string.IsNullOrEmpty(path2))
            {
                return path1;
            }

            if (path1.EndsWith("/"))
            {
                if (path2.StartsWith("/"))
                {
                    path2 = path2.Substring(1);
                }
            }
            else if (!path2.StartsWith("/"))
            {
                path1 += "/";
            }
            return path1 + path2;
        }
    }
}
